/**
 * 
 */
package com.gxa.ehome.dao.impl;




import com.gxa.ehome.dao.BaseDao;
import com.gxa.ehome.dao.UserDaoBase;
import com.gxa.ehome.entity.UserBase;
import com.gxa.ehome.util.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
//import com.gxa.userManager.entity.*;

/**
 * UserDaoImpl 作为UserDao的实现类
 * 
 * @author bilei
 * @date 2021年3月8日
 */
public class UserDaoBaseImpl extends BaseDao<UserBase> implements UserDaoBase {

	// 引入DBUtil
	private DBUtil dbutil = new DBUtil();

	@Override
	public UserBase selectById(Integer id) throws Exception {
		
		return super.selectById(id, UserBase.class);
	}

	@Override
	public UserBase selectByName(String name) throws Exception {
		String sql = "SELECT *FROM tb_user WHERE user_name=?";
		PreparedStatement preparedStatement = dbutil.getPreparedStatement(sql);
		preparedStatement.setObject(1, name);
		ResultSet resultSet = dbutil.execQuery(preparedStatement);
		UserBase user = null;
		if (resultSet.next()) {
			user = new UserBase();
			user.setId(resultSet.getInt(1));// user.setId(resultSet.getInt("id"));
			user.setName(resultSet.getString(2));
			user.setPwd(resultSet.getString(3));
			user.setUsername(resultSet.getString(4));
		}
		dbutil.closeAll();
		return user;
		
	}

	@Override
	public List<UserBase> selectAll() throws Exception {

		List<UserBase> users = new ArrayList<UserBase>();
		String sql = "SELECT * FROM tb_user";
		PreparedStatement preparedStatement = dbutil.getPreparedStatement(sql);
		ResultSet resultSet = dbutil.execQuery(preparedStatement);
//		ResultSetMetaData metaData = resultSet.getMetaData();
//		int clumn=metaData.getColumnCount();
		while (resultSet.next()) {
			UserBase user = new UserBase(resultSet.getInt("user_id"), resultSet.getString("user_name"),
					resultSet.getString("user_pwd"), resultSet.getString("user_username"));
			users.add(user);
		}
		dbutil.closeAll();
		return users;


	}

	@Override
	public List<UserBase> selectByPage(int page, int size) throws Exception {
		List<UserBase> userList = super.selectByPage(page, size, UserBase.class);
		return userList;
	}

	// 可以返回影响的行数, 还可以返回产生的自增id
	@Override
	public Integer insert(UserBase user) throws Exception {
		return super.insert(user);
	}

	@Override
	public Integer deleteById(Integer id) throws Exception {
		Integer integer = super.deleteById(id, UserBase.class);
		return integer;
	}

	@Override
	public void update(Integer id, UserBase user) throws Exception {
		// TODO Auto-generated method stub
		super.update(id,user);

	}

}
